package guoanmaker.personal.business.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.personal.business.modelnobind.DRCoupons;

@Repository
public class PersonalUserCouponsDao {
	@PersistenceContext
	EntityManager em;
	
	/**
	 * 
	* @Description: 查询推荐人通过推荐所获得优惠券列表 
	* @param  usernametj 推荐人用户名
	* @param  dateBegin 注册查询起始时间
	* @param  dateEnd 注册查询截止时间
	* @param @return
	* @author Yvonne_MGL 
	* @date 2018年3月12日 上午9:44:56     
	* @return Map    
	* @throws 
	 */
	public Map findCoupons(String usernametj,String dateBegin,String dateEnd){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String jpql="select new guoanmaker.personal.business.modelnobind.DRCoupons"
				+ "(puc.recommendedBy,c.lapseamount) from PersonalUserCoupon puc left join puc.user u left join puc.operatorCoupon c "
				+ "where 1=1 and c.vouchtype='5' ";
		if(usernametj!=null&&!usernametj.equals("")){
			jpql+=" and u.username='"+usernametj+"' ";
		}
		if(dateBegin!=null&&!dateBegin.equals("")){
			jpql+=" and puc.startDate>='"+dateBegin+"' ";
		}
		if(dateEnd!=null&&!dateEnd.equals("")){
			jpql+=" and puc.startDate<='"+dateEnd+"' ";
		}
		jpql+=" order by puc.startDate desc ";
		Query query = em.createQuery(jpql);
		List<DRCoupons> list = query.getResultList();
		Map map=new HashMap();
		map.put("totle", list.size());
		map.put("result", list);
		double totleAmount=0;
		if(list!=null&&list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				totleAmount+=list.get(i).getLapseamount();
			}
		}
		map.put("totleAmount", totleAmount);
		return map;
	}

}
